﻿using RightControl.Common;
using RightControl.IRepository;
using RightControl.IService;
using RightControl.Model;
using System.Collections.Generic;

namespace RightControl.Service
{
    public class BaseCategoryService : BaseService<BaseCategory>, IBaseCategoryService {
        public IBaseCategoryRepository repository { get; set; }

        public dynamic GetListByFilter(BaseCategory filter, PageInfo pageInfo) {
            string _where = " where 1=1";
            _where += string.Format(" and Id = {0}", filter.Id);

            return GetListByFilter(filter, pageInfo, _where);
        }

        public IEnumerable<BaseCategory> GetAllCategory() {
            return CacheHelper.GetCache<IEnumerable<BaseCategory>>("AllCategory", () => {
                return GetByWhere("where 1=1 And status=1");
            }, 7200);
        }

        public string GetCategoryName(string categoryCode) {
            Dictionary<string, BaseCategory> categoryDic = GetCategoryDictory();
            if (categoryDic != null && categoryDic.ContainsKey(categoryCode)) {
                return categoryDic[categoryCode].Label;
            }
            else {
                return "";
            }
        }

        public string GetDhCatePubId(string categoryCode) {
            Dictionary<string, BaseCategory> categoryDic = GetCategoryDictory();
            if (categoryDic != null && categoryDic.ContainsKey(categoryCode)) {
                return categoryDic[categoryCode].DhCatePubId;
            }
            else {
                return "";
            }
        }

        private Dictionary<string, BaseCategory> GetCategoryDictory() {
            return CacheHelper.GetCache<Dictionary<string, BaseCategory>>("CategoryDictory", () => {
                Dictionary<string, BaseCategory> categoryDictory = new Dictionary<string, BaseCategory>();
                IEnumerable<BaseCategory> categoryList = GetAllCategory();
                foreach (var item in categoryList) {
                    categoryDictory[item.CategoryCode] = item;
                }
                return categoryDictory;
            }, 7200);
        }
    }
}
